import pymysql


def main():
    connect = pymysql.connect(host="39.107.245.102", port=3306, user="root", passwd="wuji5201314.", db="school",
                              charset="utf8")
    try:
        with connect.cursor() as cursor:
            # 查询语句不进行赋值
            cursor.execute("select stu_id,stu_name,stu_stuaddr from tb_student")
            # cursor.fetchone()以元组的形式取值，取一个值
            print(cursor.fetchone())
            # cursor.fetchmany(数量)以元组的形式取值，取n个值
            print(cursor.fetchmany(3))
            # cursor.fetchall()以元组的形式取值，取所有值
            print(cursor.fetchall())
            # 注意：游标会随着fetch的数量而浮动
    # 当我们使用fetch的时候，拿到的数据是大元组包着小元组，真实的环境都是想要拿到大元组包裹着字典
    # 需要通过更改游标类型来实现
    except pymysql.MySQLError as error:
        print('查询失败.{}'.format(error))
    # finally:正常异常都执行，必须要释放资源
    finally:
        connect.close()


if __name__ == "__main__":
    main()